﻿/**

 @Name：layuiAdmin 用户管理，增删改查
 @Author：马达
 @Site：http://www.sunplus.wang
 @License: 杭州九宸智能科技有限公司
    
 */

layui.define(['table', 'form', 'laydate', 'upload', 'admin'], function (exports) {
    var $ = layui.$
    , admin = layui.admin
    , view = layui.view
    , table = layui.table
    , upload = layui.upload
    , laydate = layui.laydate
    , form = layui.form;

    var varButtonRight = "";
    //文章管理
    table.render({
        elem: '#LAY-app-content-list'
        , toolbar: true  //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
        , defaultToolbar: ['filter']
        , method: "post"
        , url: layui.setter.APIUrl + '/shop/orderMgr/listPain'
        , headers: { //通过 request 头传递
            Authorization: "bearer " + layui.data('layuiAdmin').access_token
        }
        , parseData: function (res) { //res 即为原始返回的数据 
            varButtonRight = res.Data.ButtonRight;
            return {
                "ResultCode": res.ResultCode, //解析接口状态
                "ResultInfo": res.ResultInfo, //解析提示文本
                "count": res.Data.TotalRecordCount, //解析数据长度
                "Data": res.Data.Items //解析数据列表 
            };
        }
    , cols: [[
      { type: 'checkbox', fixed: 'left', field: 'PID' }, //sort: true  //hide: true //  width:100
		{ field: 'User_SelSource', title: '下单人'},
		{ field: 'User_SelSourceName', title: '下单人'},                         
		{ field: 'OrderNumber', title: '订单编号'},                         
		{ field: 'OrderPrice', title: '订单金额'},                         
		{ field: 'DiscountPrice', title: '优惠金额'},                         
		{ field: 'FarePrice', title: '运费'},                         
		{ field: 'PayPrice', title: '支付金额'},                         
		{ field: 'ReturnPrice', title: '退款金额'},                         
		{ field: 'RMK', title: '备注'},                         
		{ field: 'State_Sel', title: '订单状态 ', templet: function (d) {
              var State_Sel = d.State_Sel;
              var strDes = '订单状态 [0.待支付|1.待发货|2.部分发货|3.已发货|4.已完成|5.取消]';
                var strDes = strDes.substr(strDes.indexOf("[") + 1, strDes.indexOf("]") - strDes.indexOf("[") - 1);
              var lists = strDes.split('|');
              for (var i = 0; i < lists.length; i++) {
                  if (lists[i].split('.')[0] == State_Sel) {
                      return '<button class="layui-btn layui-btn-xs">' + lists[i].split('.')[1] + "</button>";// layui-btn-primary
                  }
              }
          }, minWidth: 80, align: 'center'
      },                         
		{ field: 'AfterSaleState_Sel', title: '售后状态 ', templet: function (d) {
              var AfterSaleState_Sel = d.AfterSaleState_Sel;
              var strDes = '售后状态 [0.无售后信息|1.申请售后未处理|2.已处理|3.不予处理或取消]';
                var strDes = strDes.substr(strDes.indexOf("[") + 1, strDes.indexOf("]") - strDes.indexOf("[") - 1);
              var lists = strDes.split('|');
              for (var i = 0; i < lists.length; i++) {
                  if (lists[i].split('.')[0] == AfterSaleState_Sel) {
                      return '<button class="layui-btn layui-btn-xs">' + lists[i].split('.')[1] + "</button>";// layui-btn-primary
                  }
              }
          }, minWidth: 80, align: 'center'
      },                         
		{ field: 'DistributionState_Sel', title: '订单取消退款状态 ', templet: function (d) {
              var DistributionState_Sel = d.DistributionState_Sel;
              var strDes = '订单取消退款状态 [0.无需退款|1.待退款|2.已退款]';
                var strDes = strDes.substr(strDes.indexOf("[") + 1, strDes.indexOf("]") - strDes.indexOf("[") - 1);
              var lists = strDes.split('|');
              for (var i = 0; i < lists.length; i++) {
                  if (lists[i].split('.')[0] == DistributionState_Sel) {
                      return '<button class="layui-btn layui-btn-xs">' + lists[i].split('.')[1] + "</button>";// layui-btn-primary
                  }
              }
          }, minWidth: 80, align: 'center'
      },                         
		{ field: 'InvoiceState_Sel', title: '开票状态 ', templet: function (d) {
               var InvoiceState_Sel = d.InvoiceState_Sel;
               var strDes = '开票状态 [0.未开票|1.已开票]';
               var strDes = strDes.substr(strDes.indexOf("[") + 1, strDes.indexOf("]") - strDes.indexOf("[") - 1);
               var lists = strDes.split('|');
               if (lists[0].split('.')[0] == InvoiceState_Sel) {
                     return '<button class="layui-btn layui-btn-xs">' + lists[0].split('.')[1] + "</button>";// layui-btn-primary
               }
               else {
                    return '<button class="layui-btn layui-btn-primary layui-btn-xs">' + lists[1].split('.')[1] + "</button>";// layui-btn-primary
               }
           }, minWidth: 80, align: 'center'
		},                         
		{ field: 'ExpressState_Sel', title: '物流状态 ', templet: function (d) {
              var ExpressState_Sel = d.ExpressState_Sel;
              var strDes = '物流状态 [0.未发货|1.已发货|2.待签收|3.已签收]';
                var strDes = strDes.substr(strDes.indexOf("[") + 1, strDes.indexOf("]") - strDes.indexOf("[") - 1);
              var lists = strDes.split('|');
              for (var i = 0; i < lists.length; i++) {
                  if (lists[i].split('.')[0] == ExpressState_Sel) {
                      return '<button class="layui-btn layui-btn-xs">' + lists[i].split('.')[1] + "</button>";// layui-btn-primary
                  }
              }
          }, minWidth: 80, align: 'center'
      },                         
		{ field: 'ExamineState_Sel', title: '审核状态 ', templet: function (d) {
              var ExamineState_Sel = d.ExamineState_Sel;
              var strDes = '审核状态 [0.无需审核|1.审核中|2.审核完成|3.已驳回]';
                var strDes = strDes.substr(strDes.indexOf("[") + 1, strDes.indexOf("]") - strDes.indexOf("[") - 1);
              var lists = strDes.split('|');
              for (var i = 0; i < lists.length; i++) {
                  if (lists[i].split('.')[0] == ExamineState_Sel) {
                      return '<button class="layui-btn layui-btn-xs">' + lists[i].split('.')[1] + "</button>";// layui-btn-primary
                  }
              }
          }, minWidth: 80, align: 'center'
      },                         
		{ field: 'Sort', title: '排序'},                         
		{ field: 'UserName', title: '联系人'},                         
		{ field: 'UserCompany', title: '联系单位'},                         
		{ field: 'UserTel', title: '联系电话'},                         
		{ field: 'UserAddress', title: '联系地址'},                         
		{ field: 'DistributionMode_Sel', title: '配送方式 ', templet: function (d) {
               var DistributionMode_Sel = d.DistributionMode_Sel;
               var strDes = '配送方式 [0.快递|1.自提]';
               var strDes = strDes.substr(strDes.indexOf("[") + 1, strDes.indexOf("]") - strDes.indexOf("[") - 1);
               var lists = strDes.split('|');
               if (lists[0].split('.')[0] == DistributionMode_Sel) {
                     return '<button class="layui-btn layui-btn-xs">' + lists[0].split('.')[1] + "</button>";// layui-btn-primary
               }
               else {
                    return '<button class="layui-btn layui-btn-primary layui-btn-xs">' + lists[1].split('.')[1] + "</button>";// layui-btn-primary
               }
           }, minWidth: 80, align: 'center'
		},                         
		{ field: 'PickAddress', title: '自提点地址'},                         
		{ field: 'PickID', title: '自提点ID'},                         
		{ field: 'BillCode', title: '交易单号'},                         
		{ field: 'PayTime', title: '买家付款时间' ,templet: '<div>{{ Format(d.PayTime,"yyyy-MM-dd hh:mm:ss") }}</div>' },                         
		{ field: 'PayMode', title: '支付方式'},                         
		{ field: 'DeliverTime', title: '卖家发货时间' ,templet: '<div>{{ Format(d.DeliverTime,"yyyy-MM-dd hh:mm:ss") }}</div>' },                         
		{ field: 'ConfirmTime', title: '确认收货时间' ,templet: '<div>{{ Format(d.ConfirmTime,"yyyy-MM-dd hh:mm:ss") }}</div>' },                         
		{ field: 'IsEval', title: '是否已评价 0.未评价 1.已评价'},                         
		{ field: 'StoreID', title: '店铺ID'},                         
		{ field: 'SupplierID', title: '供应商ID'},                         
		{ field: 'GoodsData', title: '商品数据 (检索)'},                         
		{ field: 'GoodIds', title: '商品ID集合 (检索)'},                         
		{ field: 'GoodCateIds', title: '商品分类ID集合 (检索)'},                         
		{ field: 'IsUseCoupon', title: '是否使用优惠卷 [0.否|1.是]'},                         
		{ field: 'CouponInfo', title: '优惠卷使用信息'},                         
		{ field: 'IsGroupBuy', title: '是否团购单 [0.否|1.是]'},                         
		{ field: 'GroupBuyInfo', title: '团购信息'},                         
		{ field: 'SettleState', title: '结算状态 [0.未结算|1.已结算]'},                         
		{ field: 'Extend1', title: '扩展1'},                         
		{ field: 'Extend2', title: '扩展2'},                         
		{ field: 'Extend3', title: '扩展3'},                         
      {
          title: '操作', minWidth: 165, fixed: 'right', align: 'center', templet: function (d) {
              var strRtn = "";
              if (varButtonRight.indexOf('view') >= 0) {
                  strRtn += "<a class=\"layui-btn layui-btn-primary layui-btn-xs\" lay-event=\"view\">查看</a> ";
              }
              if (varButtonRight.indexOf('edit') >= 0) {
                  strRtn += "<a class=\"layui-btn layui-btn-normal layui-btn-xs\" lay-event=\"edit\">编辑</a> ";
              }
              if (varButtonRight.indexOf('del') >= 0) {
                  strRtn += "<a class=\"layui-btn layui-btn-danger layui-btn-xs\" lay-event=\"del\">删除</a> ";
              }
              return strRtn;
          }
      } 
    ]]
    , page: true
    , limit: 10
    , limits: [10, 15, 20, 25, 30]
    , text: {
        none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增 text: '对不起，加载出现异常！'
    }
    });

    //监听工具条
    table.on('tool(LAY-app-content-list)', function (obj) {
        //debugger;
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除吗？', function (index) {
                admin.req({
                    url: layui.setter.APIUrl + '/shop/orderMgr/BatchDelete' //实际使用请改成服务端真实接口
                  , data: { "ids": data.PID }
                  , type: "post"
                    //, success: function (res) {
                    //}
                  , done: function (res) {
                      layer.close(index); //执行关闭 
                      table.reload('LAY-app-content-list');
                      layer.msg('删除成功');
                  }
                });
            });
        }
        else if (obj.event === 'edit') {
            //location.hash = '/shop/shop_order/addChild/id=' + data.PID;
            admin.popup({
                title: '编辑信息'
                , area: ['850px', '650px']
              , id: 'LAY-popup-content-edit'
              , success: function (layero, index) {
                  view(this.id).render('/shop/shop_order/add', data).done(function () {
                      form.render(null, 'layuiadmin-app-form-list');

                      //监听提交
                      form.on('submit(layuiadmin-app-form-submit)', function (data) {
                          var field = data.field; //获取提交的字段 
                          //提交 Ajax 成功后，关闭当前弹层并重载表格 
                          admin.req({
                              url: layui.setter.APIUrl + '/shop/orderMgr/update' //实际使用请改成服务端真实接口
                          , data: field
                          , type: "post"
                          , done: function (res) {
                              layui.table.reload('LAY-app-content-list'); //重载表格
                              layer.close(index); //执行关闭
                              layer.msg('修改成功');
                          }
                          });
                      });
                  });
              }
            });
        }
        else if (obj.event === 'view') {
            admin.popup({
                title: '查看信息'
               , area: ['850px', '650px']
             , id: 'LAY-popup-content-view'
             , success: function (layero, index) {
                 view(this.id).render('/shop/shop_order/view', data).done(function () {
                     //监听提交
                     form.on('submit(layuiadmin-app-form-submit)', function (data) {
                         layer.close(index); //执行关闭
                     });
                 });
             }
            });
        }
    });

    exports('shop_order', {})

});
